package com.ruicar.afs.cloud.manage.insurance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.enums.ServiceClientTypeEnum;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.InsuranceBusinessTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.api.dto.BasicPrintTemplateInfoDto;
import com.ruicar.afs.cloud.manage.api.feign.info.BasicMainInfoFeign;
import com.ruicar.afs.cloud.manage.common.service.ComAttachementFileService;
import com.ruicar.afs.cloud.manage.common.vo.TemplateRuleVO;
import com.ruicar.afs.cloud.manage.insurance.entity.InsuranceTemplateInfo;
import com.ruicar.afs.cloud.manage.insurance.entity.ManageInsuranceInfo;
import com.ruicar.afs.cloud.manage.insurance.service.PrintTemplateService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * @description: 生成授权文件模板实现类
 * @author: xudongdong
 * @date: 2020/8/17
 */
@Service
@AllArgsConstructor
@Slf4j
public class PrintTemplateServiceImpl implements PrintTemplateService {

    private final BasicMainInfoFeign basicMainInfoFeign;
    private final ComPrintFormManageService comPrintFormManageService;
    private final ComAttachementFileService comAttachementFileService;

    @Override
    public String PrintTemplate(HttpServletResponse response, ManageInsuranceInfo manageInsuranceInfo, String isHave) {
        IResponse<BasicPrintTemplateInfoDto> dto = basicMainInfoFeign.basicPrintTemplateInfo(manageInsuranceInfo.getContractNo(), manageInsuranceInfo.getCarId());
        BigDecimal b = BigDecimal.valueOf(0);
        // 模板Info
        InsuranceTemplateInfo templateInfo = new InsuranceTemplateInfo();
        BeanUtils.copyProperties(manageInsuranceInfo, templateInfo);
        if (EmptyUtils.isNotEmpty(dto.getData().getAffiliatedName())) {
            templateInfo.setCustName(dto.getData().getAffiliatedName());
        }
        if (EmptyUtils.isNotEmpty(dto.getData().getSocUniCrtCode())) {
            templateInfo.setCertNo(dto.getData().getSocUniCrtCode());
        }
        templateInfo.setCarVin(dto.getData().getCarVin());
        templateInfo.setEngineNo(dto.getData().getEngineNo());
        //规则vo
        BigDecimal a = BigDecimal.valueOf(30);
        TemplateRuleVO ruleVO = new TemplateRuleVO();
        ruleVO.setLoanAmount(a);

        // 生成模板
        JSONObject jsonTemp = (JSONObject) JSONObject.toJSON(templateInfo);

        String batchPrint = null;
        if (InsuranceBusinessTypeEnum.insuranceClaim.equals(manageInsuranceInfo.getType())
                && "天津长城滨银汽车金融有限公司".equals(manageInsuranceInfo.getPayTargetAccount())) {
            if (manageInsuranceInfo.getPaymentAmount().compareTo(b) != 0) {
                ComPrintFormManage comPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
                        .eq(ComPrintFormManage::getPrintFormId, "BXLP002")
                        .eq(ComPrintFormManage::getPrintNode, "claimEndFile"));
                if ("yes".equals(isHave)) {
                    comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), AfsEnumUtil.key(ServiceClientTypeEnum.PC));
                } else {
                    comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), String.valueOf(comPrintFormManage.getId()));
                }
            } else {
                ComPrintFormManage comPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
                        .eq(ComPrintFormManage::getPrintFormId, "BXLP003")
                        .eq(ComPrintFormManage::getPrintNode, "paymentFile"));
                if ("yes".equals(isHave)) {
                    comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), AfsEnumUtil.key(ServiceClientTypeEnum.PC));
                } else {
                    comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), String.valueOf(comPrintFormManage.getId()));
                }
            }
        } else if (InsuranceBusinessTypeEnum.insuranceClaim.equals(manageInsuranceInfo.getType())) {
            ComPrintFormManage comPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
                    .eq(ComPrintFormManage::getPrintFormId, "BXLP001")
                    .eq(ComPrintFormManage::getPrintNode, "claimFile"));
            if ("yes".equals(isHave)) {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), AfsEnumUtil.key(ServiceClientTypeEnum.PC));
            } else {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), String.valueOf(comPrintFormManage.getId()));
            }
        } else if (InsuranceBusinessTypeEnum.insuranceChange.equals(manageInsuranceInfo.getType())) {
            ComPrintFormManage comPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
                    .eq(ComPrintFormManage::getPrintFormId, "BXPG001")
                    .eq(ComPrintFormManage::getPrintNode, "changeFile"));
            if ("yes".equals(isHave)) {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), AfsEnumUtil.key(ServiceClientTypeEnum.PC));
            } else {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), String.valueOf(comPrintFormManage.getId()));
            }
        } else if (InsuranceBusinessTypeEnum.insuranceExit.equals(manageInsuranceInfo.getType())) {
            ComPrintFormManage comPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
                    .eq(ComPrintFormManage::getPrintFormId, "BXTB001")
                    .eq(ComPrintFormManage::getPrintNode, "exitImageFile"));
            if ("yes".equals(isHave)) {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), AfsEnumUtil.key(ServiceClientTypeEnum.PC));
            } else {
                comPrintFormManageService.print(jsonTemp, String.valueOf(comPrintFormManage.getId()), manageInsuranceInfo.getCaseNo(), String.valueOf(comPrintFormManage.getId()));
            }
        }
        comAttachementFileService.downloadBatchFile(response, manageInsuranceInfo.getCaseNo());
        return batchPrint;
    }
}
